package cn.com.dycc.party_work.education.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.catalina.tribes.util.Arrays;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.dycc.common.controller.BaseController;
import cn.com.dycc.party_work.education.entity.DeptAndEducation;
import cn.com.dycc.party_work.education.entity.Education;
import cn.com.dycc.party_work.education.param.FindEduParam;
import cn.com.dycc.party_work.education.service.EduNotFoundException;
import cn.com.dycc.party_work.education.service.EducationService;
import cn.com.dycc.util.JsonObjectResult;

/**
 * 
 * Description: 党员教育控制层
 * Author: Liket
 * Version: 1.0
 * Create Date Time: 2018年6月21日 上午8:54:05.
 * Update Date Time: 
 * @see
 */
@Controller
@RequestMapping("/education")
public class EducationController extends BaseController {
	
	@Resource
	private EducationService eduService;

	@RequestMapping("/add.do")
	@ResponseBody
	public JsonObjectResult addEdu(Education edu,String dept_ids[],HttpServletRequest req){
		int n = eduService.addEdu(edu,dept_ids, req);
		return new JsonObjectResult(n);
	}
	
	@RequestMapping("/delete.do")
	@ResponseBody
	public JsonObjectResult deleteEdu(String education_id,HttpServletRequest req){
		int n = eduService.deleteEdu(education_id, req);
		return new JsonObjectResult(n);
	}
	
	/*@RequestMapping("/deleteall.do")
	@ResponseBody
	public JsonObjectResult deleteAllEdu(String education_ids[],HttpServletRequest req){
		int n = eduService.deleteAllEdu(education_ids, req);
		return new JsonObjectResult(n);
	}*/
	
	@RequestMapping("/update.do")
	@ResponseBody
	public JsonObjectResult updateEdu(Education edu,String dept_ids[],HttpServletRequest req){
		int n = eduService.updateEdu(edu,dept_ids, req);
		return new JsonObjectResult(n);
	}
	
	@RequestMapping("/find.do")
	@ResponseBody
	public JsonObjectResult findEdu(String education_id,HttpServletRequest req){
		Education edu = eduService.findEdu(education_id, req);
		return new JsonObjectResult(edu);
	}
	
	@RequestMapping("/findone.do")
	@ResponseBody
	public JsonObjectResult findEduById(String education_id,HttpServletRequest req){
		DeptAndEducation edu = eduService.findEduById(education_id, req);
		return new JsonObjectResult(edu);
	}
	
	@RequestMapping("/findall.do")
	@ResponseBody
	public JsonObjectResult findAllEdu(FindEduParam param,HttpServletRequest req){
		HttpSession session = req.getSession();
		String dept_id = (String) session.getAttribute("dept_id");
		param.setDept_id(dept_id);
		System.out.println(param);
		String count = eduService.findAllEduCount(param, req);
		List<DeptAndEducation> list = eduService.findAllEdu(param,req);
		return new JsonObjectResult(count,list);
	}
	
	@RequestMapping("/findallnoissue.do")
	@ResponseBody
	public JsonObjectResult findAllEduNoIssue(FindEduParam param,HttpServletRequest req){
		HttpSession session = req.getSession();
		String dept_id = (String) session.getAttribute("dept_id");
		param.setDept_id(dept_id);
		String count = eduService.findAllEduCountNoIssue(param, req);
		List<DeptAndEducation> list = eduService.findAllEduNoIssue(param,req);
		return new JsonObjectResult(count,list);
	}
	
	@RequestMapping("/issue.do")
	@ResponseBody
	public JsonObjectResult issueEdu(String education_ids[],HttpServletRequest req){
		int n = eduService.issueEdu(education_ids,req);
		return new JsonObjectResult(n);
	}
	
	@ExceptionHandler(EduNotFoundException.class)
	@ResponseBody
	public JsonObjectResult eduException(EduNotFoundException e){
		e.printStackTrace();
		return new JsonObjectResult(66,e);
	}
}
